package com.ctrip.ec.biz.dao.agent;
import com.ctrip.ec.dal.DaoBase;
import com.ctrip.ec.biz.entity.agent.AgentEntity;
import com.ctrip.ec.dal.DbAccess;
import org.springframework.stereotype.Service;
import sun.management.Agent;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.List;

@Service
public class AgentDao extends DaoBase<AgentEntity> {
    public AgentDao() {
        super(AgentEntity.class);
    }
    public AgentDao(DbAccess db) {
        super(AgentEntity.class,db);
    }

    public AgentEntity entityByAppId(String appId) throws SQLException {
        String sql = "select * from " + this.getTableName() + " where app_id =?";
        List<AgentEntity> list = this.db.query(AgentEntity.class,sql,appId);
        if(list.size()>0){
            return list.get(0);
        }else{
            return null;
        }
    }

    //代理商消费的金额,暂不做验证
    public void consume(BigDecimal paymentAmount ,Long id) throws SQLException {
        String sql = "update " + this.getTableName() + " set balance=balance-" + paymentAmount + " where id =" + id;
        this.db.execute(sql);
    }

    public AgentEntity entityByUserId(Long userId) throws SQLException {
        String sql = "select * from " + this.getTableName() + " where user_id=?";
        List<AgentEntity> list = this.db.query(AgentEntity.class,sql,userId);
        if(list.size()>0){
            return list.get(0);
        }else{
            return null;
        }
    }
}
